using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI基类 : MonoBehaviour
{
    //<string,UIBehaviour> <"控件物体名_控件名",控件>
    private Dictionary<string, UIBehaviour> 控件 = new Dictionary<string, UIBehaviour>();
    private Dictionary<string, IEventSystemHandler> 触发事件 = new Dictionary<string, IEventSystemHandler>();
    public string 面板名;
    protected virtual void Awake()
    {
        查找子UI组件<Button>();
        查找子UI组件<Image>();
        查找子UI组件<Text>();
        查找子UI组件<Slider>();
        查找子UI组件<Toggle>();
        查找子UI组件<InputField>();
        查找事件组件<UI_鼠标移入>();
    }
    
    [ContextMenu("一键重命名")]
    public virtual void 一键重命名()
    {
        重命名(GetComponentsInChildren<Text>());
        重命名(GetComponentsInChildren<Image>());
        重命名(GetComponentsInChildren<Slider>());
    }

    private void 重命名<T>(T[] ts) where T : UIBehaviour
    {
        控件 = new Dictionary<string, UIBehaviour>();
        foreach (var item in ts)
        {
            string gameName = item.gameObject.name;
            string name = $"{gameName}_{item.GetType().Name}";
            int index = 0;
            string 循环 = gameName;
            while (控件.ContainsKey(name))
            {
                index++;
                循环 = gameName + index;
                name = $"{循环}_{item.GetType().Name}";
            }
            item.gameObject.name = 循环;
            控件.Add(name, item);
        }
    }

    /// <summary>
    /// 查找子类UI控件
    /// </summary>
    /// <typeparam name="T">控件类型如Button</typeparam>
    /// <param name="子级名称">GameObject.Name</param>
    /// <returns></returns>
    public T 查找控件<T>(string 子级名称) where T : UIBehaviour
    {
        string key = $"{子级名称}_{typeof(T).Name}";
        return 控件.ContainsKey(key) ? 控件[key] as T : null;
    }

    private void 查找子UI组件<T>() where T : UIBehaviour
    {
        T[] 测试 = GetComponentsInChildren<T>();

        foreach (T item in 测试)
        {
            string name = $"{item.gameObject.name}_{item.GetType().Name}";
            if (控件.ContainsKey(name))
            {
                Debug.LogError($"已存在-<{name}>键名,请不要使在:{gameObject.name}目录下创建相同名称的对象");
                break;
            }
            控件.Add(name, item);


            if (item is Button)
                (item as Button).onClick.AddListener(() => 点击事件(item.gameObject.name));
            else if (item is Toggle)
                (item as Toggle).onValueChanged.AddListener((value) => 单选按钮(item.gameObject.name, value));
        }
    }

    private void 查找事件组件<T>() where T : MonoBehaviour,IEventSystemHandler
    {
        T[] 测试 = GetComponentsInChildren<T>();

        foreach (T item in 测试)
        {
            string name = $"{item.gameObject.name}_{item.GetType().Name}";
            if (触发事件.ContainsKey(name))
            {
                Debug.LogError($"已存在-<{name}>键名,请不要使在:{gameObject.name}目录下创建相同名称的对象");
                break;
            }
            触发事件.Add(name, item);
            if (item is UI_鼠标移入)
                (item as UI_鼠标移入).移入 += (t) => 鼠标移入事件(item.gameObject.name, t);
        }
    }

    /// <summary>
    /// 当子类按钮被点击时
    /// </summary>
    /// <param name="按钮名"></param>
    protected virtual void 点击事件(string 按钮名)
    {

    }

    /// <summary>
    /// 当子类单选框值改变时调用
    /// </summary>
    /// <param name="单选按钮名"></param>
    /// <param name="值"></param>
    protected virtual void 单选按钮(string 单选按钮名, bool 值)
    {

    }

    protected virtual void 鼠标移入事件(string 单选按钮名, RectTransform transform)
    {

    }


    //自动调用
    public virtual void 显示面板()
    {
        gameObject.SetActive(true);
    }
    //自动调用
    public virtual void 销毁面板()
    {
        UI管理器.获取实例.删除面板(gameObject.name);
    }

}
